package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.LinkModel;

import data.LinkData;

public class PaymentServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String path = ((HttpServletRequest) request).getRequestURI();
		LinkData linkData = new LinkData();
		LinkModel link = new LinkModel();
		
		long linkId = 0;
		try {
			linkId = Long.parseLong(URLDecoder.decode(path.toLowerCase().replaceAll("/payment/", ""),"UTF-8"));
		} catch (Exception e) {
			response.sendRedirect("/page-not-found");
			return;
		}
		
		link = linkData.getLink(linkId);
		
		if (link == null || !link.getType().equalsIgnoreCase("featured")) {
			response.sendRedirect("/page-not-found");
			return;
		}
		
		request.setAttribute("link", link);
		try {
			request.getRequestDispatcher("/pages/payment.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
